package com.longnuoqi.mydoctor.activity;


import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;

import com.longnuoqi.mydoctor.fragment.DoctorFragment;
import com.longnuoqi.mydoctor.fragment.NearbyFragment;
import com.longnuoqi.mydoctor.fragment.NewsFragment;
import com.longnuoqi.mydoctor.fragment.UserFragment;
import com.longnuoqi.mydoctor.fragment.ZizhenFragment;
import com.longnuoqi.mydoctor.utils.ActivityCollection;
import com.yunhe.mydoctor.R;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    private FragmentTabHost tabHost;
    private static final int[] pics = {R.drawable.tabhost_news_selector, R.drawable.tabhost_zizhen_selector,
            R.drawable.tabhost_doctor_selector, R.drawable.tabhost_nearby_selector, R.drawable.tabhost_me_selector};
    private static final Class[] fragments = {NewsFragment.class, ZizhenFragment.class,
            DoctorFragment.class, NearbyFragment.class, UserFragment.class};


    private static boolean isExit = false;//判断是否退出
    private static boolean hasTask = false;//判断是否是任务
    private Timer timer = new Timer();//声明timer对象，计时器
    private TimerTask timerTask = new TimerTask() {//为timer安排一次执行或重复执行的任务
        @Override
        public void run() {
            isExit = false;
        }
    };


    /*双击退出程序*/
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!isExit) {
                isExit = true;
                Toast.makeText(MainActivity.this, "再按一次返回桌面", Toast.LENGTH_SHORT).show();
                if (!hasTask) {
                    timer.schedule(timerTask, 2000);//安排在指定延迟后执行指定的任务
                }
            } else {
                ActivityCollection.finishAll();
                //回收资源
                System.exit(0);
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityCollection.addActivity(this);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {

        tabHost = (FragmentTabHost) findViewById(R.id.main_tabhost);
        tabHost.setup(MainActivity.this, getSupportFragmentManager(), R.id.main_framelayout);
        for (int i = 0; i < pics.length; i++) {
            TabHost.TabSpec tabSpec = tabHost.newTabSpec(pics[i] + "").setIndicator(getView(i));
            tabHost.addTab(tabSpec, fragments[i], null);
            tabHost.setBackgroundResource(R.color.colorTabhost);
        }
        tabHost.setCurrentTab(2);
    }

    private View getView(int i) {
        View view = View.inflate(MainActivity.this, R.layout.tabhost_item_layout, null);
        ImageView img = (ImageView) view.findViewById(R.id.tabhost_img);
        img.setImageResource(pics[i]);
        return view;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ActivityCollection.removeActivity(this);
    }

}
